<template>
  <div>
    <el-table
      :data="tableData2"
      style="width: 100%"
      :row-class-name="tableRowClassName">
      <el-table-column
        prop="roomName"
        label="房间名">
      </el-table-column>
      <el-table-column
        prop="currentUser"
        label="当前人数">
      </el-table-column>
      <el-table-column
        prop="roomLimit"
        label="最多人数">
      </el-table-column>
      <el-table-column
        prop="isPrivate"
        label="是否需要密码">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.roomPassword!=''">需要</el-tag>
            <el-tag v-else>不需要</el-tag>
          </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small"
                     @click="join(scope.row.roomId,scope.row.roomPassword)">加入
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-dialog title="请输入密码" :visible.sync="dialogFormVisible">
        <!--<el-form :model="form">-->
        <label>房间密码</label>
            <el-input v-model="pwd" autocomplete="off" type="password"></el-input>
          <!--</el-form-item>-->
        <!--</el-form>-->
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="passwordJoin()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
export default {
  name: 'setEvents',
  data(){
    return{
      tableData2:[],
      dialogFormVisible:false,
      currentRoomId:-1,
      pwd:''
    }
  },
  mounted(){
    this.init();
  },
  methods:{
    init(){
      this.$axios.get("/api/room/getRoomList").then(res=>{
        this.tableData2=res.data.list;
      })
    },
    join(roomId,password){
        let idList = JSON.parse(sessionStorage.getItem("roomIdList"));
        this.currentRoomId=roomId;//当前房间定位
        let id,flag=false;
        for(id in idList){
          if(this.currentRoomId == idList[id]){
            flag=true;
            break;
          }
        }
        if(flag){
          this.$router.push({path:'/enter_room',query:{id:this.currentRoomId}})

        }else if(password!=''){
          this.currentRoomId=roomId
          this.dialogFormVisible=true

        }else{
          this.$router.push({path:'/enter_room',query:{id:this.currentRoomId}})
        }
    },
    passwordJoin(){
      this.$axios.get('/RSA/getPublicKey').then(res=>{
        let encrypt = new JSEncrypt();
        encrypt.setPublicKey(res.data);
        let password = encrypt.encrypt(this.pwd);
        let userId  = sessionStorage.getItem("userId");
        this.$axios.get('/api/room/passwordLogin',{params:
            {roomId:this.currentRoomId,
              password:password,
              userId:userId}
        }).then(res=>{
          if(res.data.code==1){
            this.dialogFormVisible = false
            sessionStorage.setItem("token",res.data.token);
            this.$router.push({path:'/enter_room',query:{id:this.currentRoomId}})

          }else{
            this.$message.error('密码错误');
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
<!--$router.push({path:'/enter_room',query:{id:scope.row.roomId}})-->
